package com.atguigu.serviceedu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.serviceedu.entity.EduTeacher;
import com.atguigu.serviceedu.entity.vo.TeacherQuery;
import com.atguigu.serviceedu.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-05
 */
@RestController
@RequestMapping("/serviceedu/teacher")
//@CrossOrigin
public class EduTeacherController {
    @Autowired
    private EduTeacherService teacherService;

    @GetMapping("findAll")
    @ApiOperation("查询")
    public R list() {
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items", list);
    }

    @DeleteMapping("delete/{id}")
    public R removeById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id) {
        teacherService.removeById(id);
        return R.ok();
    }

    /**
     * 分页查询
     */
    @GetMapping("pageTeacher/{page}/{limit}")
    public R pageSelece(
            @ApiParam(name = "page", value = "当前页码", required = true) @PathVariable("page") Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable("limit") Long limit
    ) {
        Page<EduTeacher> pageParam = new Page<>(page, limit);

        teacherService.page(pageParam, null);

        List<EduTeacher> records = pageParam.getRecords();

        long total = pageParam.getTotal();

        return R.ok().data("total", total).data("rows", records);
    }


    /**
     * 组合条件分页查询
     *
     * @param page
     * @param limit
     * @param teacherquery 在前端把条件封装为对象
     * @return
     */
    @ApiOperation("组合条件分页查询")
    @PostMapping("pageTeacherCondition/{page}/{limit}")
    public R pageTeacher(
            @PathVariable("page") Long page,
            @PathVariable("limit") Long limit,
            @RequestBody(required = false) TeacherQuery teacherquery) {
        //创建page对象
        Page<EduTeacher> teacherPage = new Page<>(page,limit);

        //weaper对象用来拼接条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        //取出封装的条件
        String name = teacherquery.getName();
        Integer level = teacherquery.getLevel();
        String begin = teacherquery.getBegin();
        String end = teacherquery.getEnd();

        //对条件进行拼接
        //名称模糊查询
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_modified", end);
        }
        //模拟错误
//        try {
////            int i = 10/0;
//        }catch (Exception e){
//            throw new GuliException(20001,"谷粒错误，立即反馈...");
//        }
        wrapper.orderByDesc("gmt_create");
        //查询，封装，返回
        teacherService.page(teacherPage, wrapper);

        long total = teacherPage.getTotal();
        List<EduTeacher> records = teacherPage.getRecords();

        return R.ok().data("total", total).data("rews", records);

    }


    /**
     * 新增老师
     */
    @ApiOperation(value = "新增讲师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {

        boolean save = teacherService.save(eduTeacher);

        if (save) return R.ok();
        else return R.error();
    }

    /**
     * 通过id查询
     */
    @GetMapping("selectTeacher/{id}")
    public R getTeacher(@PathVariable(value = "id", required = true) String id) {

        EduTeacher teacher = teacherService.getById(id);

        return R.ok().data("teacher", teacher);
    }

    /**
     * 修改
     */
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher teacher) {

        boolean flag = teacherService.updateById(teacher);

        if (flag) return R.ok();
        else return R.error();
    }


}

